<?php

namespace App\Exceptions;

use Illuminate\Database\QueryException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Laravel\Lumen\Exceptions\Handler as ExceptionHandler;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Throwable;
use Illuminate\Auth\AuthenticationException;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
use Tymon\JWTAuth\Exceptions\TokenExpiredException;
use Tymon\JWTAuth\Exceptions\TokenInvalidException;
use Tymon\JWTAuth\Exceptions\JWTException;
use App\Exceptions\LogicException;

class Handler extends ExceptionHandler
{
    protected $dontReport = [
        LogicException::class,
        AuthenticationException::class,
        MethodNotAllowedHttpException::class,
        NotFoundHttpException::class,
    ];

    public function render($request, Throwable $exception)
    {
        // 处理认证异常
        if ($exception instanceof AuthenticationException) {
            return response()->json([
                'code' => 401,
                'message' => '未登录或登录已过期',
                'data' => []
            ], 401);
        }

        // 处理未授权异常
        if ($exception instanceof UnauthorizedHttpException) {
            return response()->json([
                'code' => 401,
                'message' => '未授权访问',
                'data' => []
            ], 401);
        }

        // 处理 JWT Token 相关异常
        if ($exception instanceof TokenExpiredException) {
            return response()->json([
                'code' => 401,
                'message' => 'Token 已过期',
                'data' => []
            ], 401);
        }

        if ($exception instanceof TokenInvalidException) {
            return response()->json([
                'code' => 401,
                'message' => '无效的 Token',
                'data' => []
            ], 401);
        }

        if ($exception instanceof JWTException) {
            return response()->json([
                'code' => 401,
                'message' => 'Token 解析失败',
                'data' => []
            ], 401);
        }

        // 处理业务逻辑错误
        if ($exception instanceof LogicException) {
            return response()->json([
                'code' => $exception->getCode(),
                'message' => $exception->getMessage(),
                'data' => $exception->getData()
            ]);
        }

        // 处理路由未找到异常
        if ($exception instanceof NotFoundHttpException || $exception instanceof MethodNotAllowedHttpException) {
            return response()->json([
                'code' => 404,
                'message' => '接口不存在',
                'data' => []
            ]);
        }

        // 处理数据库相关异常
        if ($exception instanceof QueryException) {
            return response()->json([
                'code' => 501,
                'message' => '服务器错误',
                'data' => [
                    'error_type' => 'database_error',
                    'error_message' => $exception->getMessage(),
                ]
            ]);
        }

        // 处理模型未找到异常
        if ($exception instanceof ModelNotFoundException) {
            return response()->json([
                'code' => 404,
                'message' => '资源未找到',
                'data' => []
            ]);
        }

        // 其他未预期的异常
        return response()->json([
            'code' => 500,
            'message' => '服务器错误' . $exception->getMessage(),
            'data' => []
        ]);
    }
}
